package lib;

import static util.Constant.FILE_COMMENTS;
import static util.Constant.PATH_DATA;
import static util.Constant.STR_NAME_NULL;
import static util.Tool.getMaxID;
import io.InOutput;

import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;

import base.Comment;
import base.User;

public class CommentManager {

	/**
	 * The {@code EventManager} instance.
	 */
	private static CommentManager managerInstance;

	/**
	 * Get an instance of {@code EventManager}.
	 * 
	 * @return an instance of {@code EventManager}.
	 */
	public static CommentManager getInstance() {
		if (managerInstance == null) {
			managerInstance = new CommentManager();
		}
		return managerInstance;
	}

	/**
	 * The {@code Hashtable} containing the Event object.
	 */
	private Hashtable<Integer, Object> commentTable;

	/**
	 * The {@code String} containing the path of Event data.
	 */
	private String path = PATH_DATA + FILE_COMMENTS;

	/**
	 * Constructor of the {@code class}. Create the user manager.
	 * 
	 * @throws Exception
	 */
	private CommentManager() {
		initialize();
		try {
			this.load();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create a new Event. The date will be current time.
	 * 
	 * @param content
	 *            the comment content
	 * @param owner
	 *            The User who write the comment
	 * 
	 * @return Integer the ID of created event
	 * 
	 * @throws IllegalArgumentException
	 */
	public Comment create(String content, User owner) {
		return this.create(content, owner, new GregorianCalendar());
	}

	/**
	 * Create a new Event
	 * 
	 * @param content
	 *            the comment content
	 * @param owner
	 *            The User who write the comment
	 * @param date
	 *            The date of the comment
	 * 
	 * @return Integer the ID of created event
	 * 
	 * @throws IllegalArgumentException
	 */
	public Comment create(String content, User owner, GregorianCalendar date) {
		if (content == null || content.trim().isEmpty())
			throw new IllegalArgumentException(STR_NAME_NULL);
		Integer ID = getMaxID(commentTable) + 1;
		Comment comment = new Comment(ID, owner.getID(), content, date);
		commentTable.put(ID, comment);
		save();
		return comment;
	}

	/**
	 * Get a comment from his ID.
	 * 
	 * @param ID
	 *            The Comment ID.
	 */
	public Comment get(Integer ID) {
		return (commentTable.get(ID) == null ? null : (Comment) commentTable
				.get(ID));
	}

	public Set<Comment> getAll() {
		Set<Comment> comments = new HashSet<Comment>();
		for (Object obj : commentTable.values()) {
			comments.add((Comment) obj);
		}
		return comments;
	}

	/**
	 * Initialize the instance.
	 */
	public void initialize() {
		commentTable = new Hashtable<Integer, Object>();
	}

	/**
	 * Load the database in comment table.
	 * 
	 * @throws Exception
	 */
	private void load() throws Exception {
		commentTable = InOutput.read(path);
	}

	/**
	 * Remove a comment.
	 * 
	 * @param ID
	 *            The Comment ID.
	 */
	public Comment remove(Integer ID) {
		Comment retour = (Comment) commentTable.remove(ID);
		save();
		return retour;

	}

	/**
	 * Save the comment table in database.
	 */
	private void save() {
		InOutput.write(commentTable, path);
	}

	/**
	 * Set the path.
	 * 
	 * @param path
	 *            the path to set
	 */
	public void setPath(String path) {
		this.path = path;
	}
}
